<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="记录金额" prop="money">
      <el-input v-model="dataForm.money" placeholder="记录金额"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="记录时间" prop="time">
      <el-input v-model="dataForm.time" placeholder="记录时间"></el-input>
    </el-form-item>
    <el-form-item label="类型，0支出，1收入" prop="type">
      <el-input v-model="dataForm.type" placeholder="类型，0支出，1收入"></el-input>
    </el-form-item>
    <el-form-item label="收支类型编码" prop="typeCode">
      <el-input v-model="dataForm.typeCode" placeholder="收支类型编码"></el-input>
    </el-form-item>
    <el-form-item label="收支类型图标" prop="typeIcon">
      <el-input v-model="dataForm.typeIcon" placeholder="收支类型图标"></el-input>
    </el-form-item>
    <el-form-item label="收支类型名称" prop="typeName">
      <el-input v-model="dataForm.typeName" placeholder="收支类型名称"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="author">
      <el-input v-model="dataForm.author" placeholder="创建人"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新人" prop="modifier">
      <el-input v-model="dataForm.modifier" placeholder="更新人"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="modifyTime">
      <el-input v-model="dataForm.modifyTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="状态,1正常，0无效，-1删除" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态,1正常，0无效，-1删除"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          money: '',
          remark: '',
          time: '',
          type: '',
          typeCode: '',
          typeIcon: '',
          typeName: '',
          author: '',
          createTime: '',
          modifier: '',
          modifyTime: '',
          status: ''
        },
        dataRule: {
          money: [
            { required: true, message: '记录金额不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          time: [
            { required: true, message: '记录时间不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '类型，0支出，1收入不能为空', trigger: 'blur' }
          ],
          typeCode: [
            { required: true, message: '收支类型编码不能为空', trigger: 'blur' }
          ],
          typeIcon: [
            { required: true, message: '收支类型图标不能为空', trigger: 'blur' }
          ],
          typeName: [
            { required: true, message: '收支类型名称不能为空', trigger: 'blur' }
          ],
          author: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          modifier: [
            { required: true, message: '更新人不能为空', trigger: 'blur' }
          ],
          modifyTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态,1正常，0无效，-1删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/ledger/record/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.money = data.record.money
                this.dataForm.remark = data.record.remark
                this.dataForm.time = data.record.time
                this.dataForm.type = data.record.type
                this.dataForm.typeCode = data.record.typeCode
                this.dataForm.typeIcon = data.record.typeIcon
                this.dataForm.typeName = data.record.typeName
                this.dataForm.author = data.record.author
                this.dataForm.createTime = data.record.createTime
                this.dataForm.modifier = data.record.modifier
                this.dataForm.modifyTime = data.record.modifyTime
                this.dataForm.status = data.record.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/ledger/record/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'money': this.dataForm.money,
                'remark': this.dataForm.remark,
                'time': this.dataForm.time,
                'type': this.dataForm.type,
                'typeCode': this.dataForm.typeCode,
                'typeIcon': this.dataForm.typeIcon,
                'typeName': this.dataForm.typeName,
                'author': this.dataForm.author,
                'createTime': this.dataForm.createTime,
                'modifier': this.dataForm.modifier,
                'modifyTime': this.dataForm.modifyTime,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
